package com.jx.smartbot.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Base64;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import springfox.documentation.annotations.ApiIgnore;
@ApiIgnore
@Controller
@RequestMapping("/biz/gocomplus")  
public class GoComPlusController {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(GoComPlusController.class);
	@Resource(name="clientUrl")
	private String clientUrl;
	public void init(){}
	//sharing
	@CrossOrigin(origins = "*")
	@RequestMapping(value = "/sharing", method = RequestMethod.POST)
	public Object wp_sharing(@RequestParam(value = "token", defaultValue = "") String token,
			@RequestBody  String Datajson,
			HttpServletRequest reques,HttpServletResponse response)throws IOException{
		String clientUrl_=this.str("zhide/sharetozhide",token,Datajson);
		logger.info(clientUrl_);
		response.sendRedirect(clientUrl_);
		return null;
	}
	//blog
	@CrossOrigin(origins = "*")
	@RequestMapping(value = "/blog", method = RequestMethod.POST)
	public Object wp_blog(@RequestParam(value = "token", defaultValue = "") String token,
			@RequestBody  String Datajson,
			HttpServletRequest reques,HttpServletResponse response)throws IOException{
		String clientUrl_=this.str("zhide/",token,Datajson);
		logger.info(clientUrl_);
		response.sendRedirect(clientUrl_);
		return null;
	}
	
	private String str(String htmlfile,String token,String Datajson){
		String clientUrl_=clientUrl+htmlfile+"?token=gocomplus_"+token;
		String urlEncoded ="";
		try {
			 urlEncoded = Base64.getEncoder().encodeToString(Datajson.getBytes("utf-8"));
			 urlEncoded=URLEncoder.encode(urlEncoded,"utf-8").replaceAll("%2B", "%2b");
			 urlEncoded ="&data=" +  urlEncoded;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return clientUrl_+urlEncoded;
	}
}

